package com.bignippleboy.recordthelife.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bignippleboy.recordthelife.LifeRecordItem;
import com.bignippleboy.recordthelife.LifeRecordManager;
import com.bignippleboy.recordthelife.R;

public class ItemAdapter extends BaseAdapter {

    private Context mContext;
	private LifeRecordManager lifeRecordManager = LifeRecordManager
	.getInstance();
	
	Bitmap bitmap=null;

	public ItemAdapter(Context mContext) {
		super();
		this.mContext = mContext;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lifeRecordManager.getmLifeRecords().size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
        return lifeRecordManager.getmLifeRecords().get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
//        LifeRecordItem s = lifeRecordManager.getmLifeRecords().get(position);
        return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        if (convertView == null) {
            // Make up a new view
            LayoutInflater inflater = (LayoutInflater) mContext
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.liferecord_item, null);
        } else {
            // Use convertView if it is available
            view = convertView;
        }
        LifeRecordItem life = lifeRecordManager.getmLifeRecords().get(position);

        ImageView i = (ImageView) view.findViewById(R.id.image);
        i.setScaleType(ImageView.ScaleType.FIT_XY);
        
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inSampleSize = 4;
        if(life.getPhoto() == null || life.getPhoto() == "")
        	bitmap = BitmapFactory.decodeResource(mContext.getResources(),
					R.drawable.beauty);
        else
        	bitmap = BitmapFactory.decodeFile(life.getPhoto(), opts);
        if(bitmap.getHeight() > bitmap.getWidth()) {
        	i.setLayoutParams(new LinearLayout.LayoutParams(100, 150));
        } else {
        	i.setLayoutParams(new LinearLayout.LayoutParams(150, 100));
        }
        i.setImageBitmap(bitmap);
        i.setBackgroundResource(R.drawable.picture_frame);
        
        TextView t = (TextView) view.findViewById(R.id.comment);
        t.setText(life.getComment());
        
        t = (TextView) view.findViewById(R.id.date);
        t.setText(life.getDatetime());
        return view;
    }

}
